package com.example.shell.command;

import cn.hutool.core.util.StrUtil;
import com.example.shell.config.FileConfig;
import com.example.shell.console.ConsoleTable;
import com.example.shell.model.entity.AttendanceRecord;
import com.example.shell.model.entity.UserInfo;
import com.example.shell.model.query.CheckinListQuery;
import com.example.shell.service.OaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.context.InteractionMode;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

@ShellComponent
@ShellCommandGroup("oa")
public class OaCommand {
    @Autowired
    private OaService oaService;

    FileConfig fileConfig = new FileConfig();

    @ShellMethod(key = "login", value = "登录", group = "oa",
            interactionMode = InteractionMode.INTERACTIVE)
    public String login(
            @ShellOption(defaultValue = "")String username,
            @ShellOption(defaultValue = "")String password) {
        if (StrUtil.isBlank(username)){
            username = fileConfig.get("username");
            password = fileConfig.get("password");
            if (StrUtil.isBlank(username) || StrUtil.isBlank(password)){
                return "账号密码错误，请正确检查输入或配置文件！";
            }
        }

        UserInfo login = oaService.login(username, password);

        // 持久化
        fileConfig.set("LoginIdentity",login.getLoginIdentity());
        fileConfig.set("username",username);
        fileConfig.set("password",password);
        fileConfig.write();

        return login.getUserCode();
    }

    @ShellMethod(key = "list", value = "打卡列表", group = "oa",
            interactionMode = InteractionMode.INTERACTIVE)
    public void list(
            @ShellOption(defaultValue = "")String m
    ) {
        CheckinListQuery query = new CheckinListQuery();
        query.setPage(1);
        query.setSize(31);

        // 获取当前时间
        LocalDateTime date;
        if(StrUtil.isBlank(m)){
            date = LocalDateTime.now();
        }else {
            date = LocalDateTime.of(2025,Integer.parseInt(m),1,0,0);
        }

        // 获取第一天时间
        LocalDateTime firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        // 获取最后一天时间
        LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());

        // 设置转换格式 年月日
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        query.setStartDate(fmt.format(firstDay));
        query.setEndDate(fmt.format(lastDay));
        query.setEmployeeCode(fileConfig.get("username"));
        List<AttendanceRecord> attendanceRecords = oaService.checkinList(query);
        print(attendanceRecords);
    }


    public void print(List<AttendanceRecord> attendanceRecords) {
        // 示例数据（序号，工号，日期，上班时间，下班时间）
        String[][] data = new String[attendanceRecords.size()][7];

        for (int i = 0; i < attendanceRecords.size(); i++) {
            AttendanceRecord item = attendanceRecords.get(i);
            data[i][0] = String.valueOf(i + 1);
            data[i][1] = String.valueOf(item.getSEmployeeCode());
            data[i][2] = String.valueOf(item.getDDate());
            data[i][3] = String.valueOf(item.getStart());
            data[i][4] = String.valueOf(item.getEnd());
            data[i][5] = String.valueOf(item.getStatus());
            data[i][6] = String.valueOf(item.getDanger());
        }


        // 列宽定义
        int[] columnWidths = {5, 8, 13, 13, 13, 6, 6}; // 序号(6), 工号(8), 日期(12), 上班(10), 下班(10)
        String[] columnTitles = new String[]{"index", "work no", "date", "work time", "off-work time", "status", "danger"};
        // 打印表头
        ConsoleTable.printRow(columnWidths, columnTitles);
        ConsoleTable.printHorizontalLine(columnWidths);

        // 打印数据行
        for (String[] row : data) {
            ConsoleTable.printRow(columnWidths, row);
        }
        ConsoleTable.printHorizontalLine(columnWidths);
    }
}
